﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 责任链模式
{
    /// <summary>
    /// 处理请求类 （抽象的）
    /// </summary>
    public abstract class Approve
    {
        /// <summary>
        /// 下一个同意的人（一般是上司）      
        /// </summary>
        public Approve nextApprove;     

        public abstract void ProcessRequest(RequestMoney request);
    }

    /// <summary>
    /// 第一个具体的处理请求类（经理）
    /// </summary>
    public class Manager : Approve
    {
        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public Manager(string name)
        {
            Name = name;
        }
        public override void ProcessRequest(RequestMoney request)
        {
            if (request.Money < 10000)
            {
                Console.WriteLine("{0}-{1}同意这个购买请求", this.GetType().Name, name);
            }
            else if (nextApprove != null)   //此处的nextApprove源于基类
            {
                //客户端设置manager.nextApprove = vpre;后，此处
                //就是调用VicePresident.ProcessRequest(request)了
                nextApprove.ProcessRequest(request);
            }
        }
    }

    /// <summary>
    /// 第二个具体的处理请求类（副总）
    /// </summary>
    public class VicePresident : Approve
    {
        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public VicePresident(string name)
        {
            Name = name;
        }
        public override void ProcessRequest(RequestMoney request)
        {
            if (request.Money < 25000)
            {
                Console.WriteLine("{0}-{1}同意这个购买请求", this.GetType().Name, name);
            }
            else if (nextApprove != null)
            {
                nextApprove.ProcessRequest(request);
            }
        }
    }

    /// <summary>
    /// 第三个具体的处理请求类（总经理）
    /// </summary>
    public class President : Approve
    {
        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public President(string name)
        {
            Name = name;
        }
        public override void ProcessRequest(RequestMoney request)
        {
            if (request.Money < 100000)
            {
                Console.WriteLine("{0}-{1}同意这个购买请求", this.GetType().Name, name);
            }
            else
            {
                Console.WriteLine("Request需要组织一个会议讨论");
            }
        }
    }
}
